﻿using System;
using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Shop.Base.Entity;
using Shop.Base.Entity.Insert;
using Shop.Base.Entity.Select;
using Shop.Base.Entity.Update;
using Shop.Base.Interface;
using Shop.Entity;
using Shop.Tools;
using Shop.Web.Models;

namespace Shop.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class SystemButtonController : Controller
    {
        // GET
        private IMapper mapper;
        private ISystemButton button;

        public SystemButtonController(IMapper mapper, ISystemButton _button)
        {
            this.mapper = mapper;
            this.button = _button;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetList(string name,  int isDel = -1, int pageIndex = 1, int pageSize = 15)
        {
            try
            {
                var list = button.GetPage(name,  isDel, pageIndex, pageSize, out int total);
                var showList = mapper.Map<List<SystemButtonPo>, List<ShowSystemButton>>(list);
                return Json(new ResultModel
                {
                    code = OperationResultType.GetList,
                    data = new { list = showList, total },
                    error_msg = "",
                    success = true
                });
            }
            catch (Exception e)
            {
                Tools.StaticConst.log.Error(e);
                return Json(new ResultModel { code = OperationResultType.Throws, data = new List<string>(), error_msg = "查询异常", success = false });
            }
        }

        public IActionResult Edit(Guid? buttonId)
        {
            var comUser = HttpContext.Session.Get<ComUser>(SessionKeys.UserInfo);
            if (buttonId.HasValue && !buttonId.Value.IsEmptyGuid())
            {
                var buttons= button.Find<SystemButtonPo>(buttonId.Value);
                ViewBag.model = buttons.ToJsonData();
            }
            else
            {
                ViewBag.model = new AddSystemButton { CreateId = comUser.Id, CreateName = comUser.UserName }.ToJsonData();
            }
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit([FromForm]SystemButtonPo model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var comUser = HttpContext.Session.Get<ComUser>(SessionKeys.UserInfo);
                    if (model.Id.IsEmptyGuid())
                    {
                        var inserts = mapper.Map<SystemButtonPo, AddSystemButton>(model);
                        button.Insert(inserts, comUser);
                    }
                    else
                    {
                        var updates = mapper.Map<SystemButtonPo, UpdateSystemButton>(model);
                        button.Update(updates, comUser);
                    }
                    return RedirectToAction("Index");
                }
                ViewBag.model = model.ToJsonData();
                TempData["errMsg"] = new JsMessage("无效的操作", 5, JsMessageType.Warning).ToString();
                return View();
            }
            catch (Exception e)
            {
                Tools.StaticConst.log.Error(e);
                ViewBag.model = model.ToJsonData();
                TempData["errMsg"] = new JsMessage("保存出现异常", 5, JsMessageType.Danger).ToString();
                return View();
            }
        }

        [HttpPost]
        public IActionResult Delete(Guid id)
        {
            try
            {
                if (id.IsEmptyGuid()) return Json(new ResultModel { code = OperationResultType.ParamError, error_msg = "指定删除的按钮不存在", success = false });
                var comUser = HttpContext.Session.Get<ComUser>(SessionKeys.UserInfo);
                button.Delete<UpdateSystemButton>(id, comUser);
                return Json(new ResultModel { code = OperationResultType.Success, error_msg = "删除成功", success = true });
            }
            catch (Exception e)
            {
                Tools.StaticConst.log.Error(e);
                return Json(new ResultModel { code = OperationResultType.Throws, error_msg = "系统异常，请刷新再试", success = false });
            }
        }
    }
}